

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/my-blog/img/fluid.png">
  <link rel="icon" href="/my-blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="木舟">
  <meta name="keywords" content="python Vue Test 测试开发 自动化测试">
  
    <meta name="description" content="元组tuple 不可变数据类型，元素的值不可以修改的，Python序列数据类型，可以存储多个元素，通过()括起来，每个元素之间通过,隔开，可以为任意类型 1234tup1&#x3D;(&amp;#x27;NN&amp;#x27;,221,(1,2),[1,2])print(type(tup1))tup2&#x3D;(&amp;#x27;NULL&amp;#x27;,)print(type(tup2))  获取元组中的某个值123456# 下标tup">
<meta property="og:type" content="article">
<meta property="og:title" content="python基础（下）">
<meta property="og:url" content="https://walker_kwu.gitee.io/my-blog/2023/02/19/python/base/python%E5%9F%BA%E7%A1%80%EF%BC%88%E4%B8%8B%EF%BC%89/">
<meta property="og:site_name" content="My Blog">
<meta property="og:description" content="元组tuple 不可变数据类型，元素的值不可以修改的，Python序列数据类型，可以存储多个元素，通过()括起来，每个元素之间通过,隔开，可以为任意类型 1234tup1&#x3D;(&amp;#x27;NN&amp;#x27;,221,(1,2),[1,2])print(type(tup1))tup2&#x3D;(&amp;#x27;NULL&amp;#x27;,)print(type(tup2))  获取元组中的某个值123456# 下标tup">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-02-19T08:34:17.000Z">
<meta property="article:modified_time" content="2023-05-23T10:37:53.265Z">
<meta property="article:author" content="木舟">
<meta property="article:tag" content="测试开发">
<meta property="article:tag" content="Python基础">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>python基础（下） - My Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/my-blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/my-blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/my-blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"walker_kwu.gitee.io","root":"/my-blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/my-blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/my-blog/js/utils.js" ></script>
  <script  src="/my-blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/my-blog/">
      <strong>木舟</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/my-blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="python基础（下）"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        木舟
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-02-19 16:34" pubdate>
          2023年2月19日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.1k 字
        
      </span>
    

    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">python基础（下）</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2023年5月23日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="元组"><a href="#元组" class="headerlink" title="元组"></a>元组</h1><p>tuple</p>
<p>不可变数据类型，元素的值不可以修改的，Python序列数据类型，可以存储多个元素，通过()括起来，<br>每个元素之间通过,隔开，可以为任意类型</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python">tup1=(<span class="hljs-string">&#x27;NN&#x27;</span>,<span class="hljs-number">221</span>,(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>),[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>])<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">type</span>(tup1))<br>tup2=(<span class="hljs-string">&#x27;NULL&#x27;</span>,)<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">type</span>(tup2))<br></code></pre></td></tr></table></figure>

<h2 id="获取元组中的某个值"><a href="#获取元组中的某个值" class="headerlink" title="获取元组中的某个值"></a>获取元组中的某个值</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 下标</span><br>tup1=(<span class="hljs-string">&#x27;NN&#x27;</span>,<span class="hljs-number">221</span>,<span class="hljs-number">246</span>)<br><span class="hljs-built_in">print</span>(tup1[<span class="hljs-number">1</span>])<br><span class="hljs-comment"># 切片,和字符串切片一样</span><br>tup1=(<span class="hljs-string">&#x27;NN&#x27;</span>,<span class="hljs-number">221</span>,<span class="hljs-number">246</span>)<br><span class="hljs-built_in">print</span>(tup1[<span class="hljs-number">1</span>:<span class="hljs-number">2</span>])<br></code></pre></td></tr></table></figure>

<h2 id="修改元组的值"><a href="#修改元组的值" class="headerlink" title="修改元组的值"></a>修改元组的值</h2><p>不可以修改，但是可以通过+或*连接组合， in/not in判断是否有对应元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python">tup1=(<span class="hljs-string">&#x27;NN&#x27;</span>,<span class="hljs-number">221</span>,<span class="hljs-number">246</span>)<br>tup3=(<span class="hljs-string">&#x27;MS246&#x27;</span>,)<br>new=tup1[<span class="hljs-number">0</span>:<span class="hljs-number">2</span>]+tup3<br><span class="hljs-built_in">print</span>(new)<br>new2 = tup3*<span class="hljs-number">2</span><br><span class="hljs-built_in">print</span>(new2)<br></code></pre></td></tr></table></figure>

<h2 id="常用方法"><a href="#常用方法" class="headerlink" title="常用方法"></a>常用方法</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python">tup = (<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">7</span>, <span class="hljs-number">34</span>, <span class="hljs-number">74</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">24</span>, <span class="hljs-number">56</span>, <span class="hljs-number">36</span>, <span class="hljs-number">86</span>, <span class="hljs-number">35</span>, <span class="hljs-number">77</span>)<br><span class="hljs-built_in">print</span>(tup.count(<span class="hljs-number">3</span>)) <span class="hljs-comment"># 统计指定元素有几个</span><br><span class="hljs-built_in">print</span>(tup.index(<span class="hljs-number">3</span>)) <span class="hljs-comment"># 获取指定元素下标，只会获得第一个</span><br></code></pre></td></tr></table></figure>

<h1 id="列表"><a href="#列表" class="headerlink" title="列表"></a>列表</h1><p>序列数据类型，类似于tuple元组，他们之间区别：元组是不可变，list可变数据类型，列表可以存储多<br>个元素，元素与元素之间通过，隔开，通过[]表示,元素的类型可以为任意类型</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python">list1=[<span class="hljs-string">&#x27;jeffery&#x27;</span>,<span class="hljs-string">&#x27;MS246&#x27;</span>,<span class="hljs-string">&#x27;MS240&#x27;</span>]<br><span class="hljs-built_in">print</span>(list1)<br>list2=[]<br><span class="hljs-built_in">print</span>(list2)<br>list4=[<span class="hljs-string">&#x27;01&#x27;</span>,<span class="hljs-string">&#x27;02&#x27;</span>,[<span class="hljs-string">&#x27;03&#x27;</span>,<span class="hljs-string">&#x27;NN&#x27;</span>]]<br><span class="hljs-built_in">print</span>(list4)<br>l2 = <span class="hljs-built_in">list</span>(<span class="hljs-string">&#x27;fjfdsklagjsflag&#x27;</span>)<br><span class="hljs-built_in">print</span>(l2)<br></code></pre></td></tr></table></figure>

<h2 id="获取列表中的元素"><a href="#获取列表中的元素" class="headerlink" title="获取列表中的元素"></a>获取列表中的元素</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 下标</span><br>list1=[<span class="hljs-string">&#x27;NN&#x27;</span>,<span class="hljs-number">221</span>,<span class="hljs-number">246</span>]<br><span class="hljs-built_in">print</span>(list1[<span class="hljs-number">1</span>])<br><span class="hljs-comment"># 切片,和字符串切片一样</span><br>list2=[<span class="hljs-string">&#x27;NN&#x27;</span>,<span class="hljs-number">221</span>,<span class="hljs-number">246</span>]<br><span class="hljs-built_in">print</span>(list2[<span class="hljs-number">1</span>:<span class="hljs-number">2</span>:<span class="hljs-number">2</span>])<br><span class="hljs-built_in">print</span>(list2[::-<span class="hljs-number">1</span>])<br></code></pre></td></tr></table></figure>

<h2 id="修改列表元素的值"><a href="#修改列表元素的值" class="headerlink" title="修改列表元素的值"></a>修改列表元素的值</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 按照索引改值</span><br>l1[<span class="hljs-number">0</span>] = <span class="hljs-string">&#x27;男神&#x27;</span><br><span class="hljs-comment"># 按照切片改（了解）</span><br>l1[<span class="hljs-number">2</span>:] = <span class="hljs-string">&#x27;fsdafsdafsdfdsfsadfdsfdsgsfdag&#x27;</span><br><span class="hljs-built_in">print</span>(l1)<br><span class="hljs-comment"># 按照切片（步长）（了解）</span><br>l1[::<span class="hljs-number">2</span>] = <span class="hljs-string">&#x27;abc&#x27;</span><br>l1[::<span class="hljs-number">2</span>] = <span class="hljs-string">&#x27;abcd&#x27;</span><br><span class="hljs-built_in">print</span>(l1)<br></code></pre></td></tr></table></figure>

<h2 id="删除列表元素或者列表"><a href="#删除列表元素或者列表" class="headerlink" title="删除列表元素或者列表"></a>删除列表元素或者列表</h2><p>这里通过 <code>del</code> 来删除, 但实际上列表有自带的函数可以进行元素删除</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 删除某个元素</span><br><span class="hljs-keyword">del</span> list1[-<span class="hljs-number">1</span>]<br><span class="hljs-built_in">print</span>(list1)<br><span class="hljs-comment"># 删除整个列表</span><br>list3=[<span class="hljs-string">&#x27;abcdef&#x27;</span>]<br><span class="hljs-keyword">del</span> list3<br></code></pre></td></tr></table></figure>

<h2 id="list常用的运算符"><a href="#list常用的运算符" class="headerlink" title="list常用的运算符"></a>list常用的运算符</h2><p><code>+ *  in/not in</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python">new = [<span class="hljs-string">&quot;A&quot;</span>,<span class="hljs-string">&#x27;B&#x27;</span>] * <span class="hljs-number">2</span><br>name211 = list1 + new<br><span class="hljs-built_in">print</span>(name211)<br></code></pre></td></tr></table></figure>

<h2 id="常用函数"><a href="#常用函数" class="headerlink" title="常用函数"></a>常用函数</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 列表的常用方法</span><br><span class="hljs-comment"># list1 = [&#x27;NN&#x27;, 221, &#x27;NN&#x27;, 246, &#x27;MS246&#x27;]</span><br><span class="hljs-comment"># list1.append(&#x27;坚果&#x27;)  # 列表最后添加新元素（追加新元素）</span><br><span class="hljs-comment"># list1.extend([6, 7, 8])  # 追加多个元素</span><br><span class="hljs-comment"># list1.insert(0, 333)  # 指定索引位置，添加元素</span><br><span class="hljs-comment"># value = list1.pop()  # 默认移除最后的一个元素，可以指定下标，返回删除的值</span><br><span class="hljs-comment"># list1.remove(&#x27;NN&#x27;)  # 根据指定的值进行移除，,如果有重名元素，默认删除从左数第一个</span><br><span class="hljs-comment"># list1.reverse()  # 反转列表元素</span><br><span class="hljs-comment"># list1 = [123, 221, 0, 246, 56]</span><br><span class="hljs-comment"># list1.sort()  # 排序  默认升序</span><br><span class="hljs-comment"># list1.sort(reverse=True)  # 降序</span><br><span class="hljs-comment"># list1.clear()  # 清空列表元素</span><br><span class="hljs-comment"># index_value = list1.index(&#x27;MS246&#x27;)  # 获取元素下标</span><br><span class="hljs-comment"># print(index_value)</span><br><span class="hljs-comment"># count_value = list1.count(&#x27;NN&#x27;)  # 统计列表有几个指定元素</span><br><span class="hljs-comment"># print(count_value)</span><br><span class="hljs-comment"># tup1 = (1, 2, 3)</span><br><span class="hljs-comment"># print(type(tup1))</span><br><span class="hljs-comment"># list2 = list(tup1) # 元组转换列表</span><br><span class="hljs-comment"># print(list2)</span><br><span class="hljs-comment"># print(type(list2))</span><br><span class="hljs-comment"># len_value = len(list1)  # 获取列表长度</span><br><span class="hljs-comment"># print(len_value)</span><br><span class="hljs-comment"># print(list1)</span><br><span class="hljs-comment"># list1 = [123, 221, 0, 246, 56]</span><br><span class="hljs-comment"># print(min(list1))  # 获取最小值</span><br><span class="hljs-comment"># print(max(list1))  # 获取最大值</span><br></code></pre></td></tr></table></figure>

<h2 id="列表推导式（了解）"><a href="#列表推导式（了解）" class="headerlink" title="列表推导式（了解）"></a>列表推导式（了解）</h2><p>[表达式 for 变量 in 旧列表] 或者 [表达式 for 变量 in 旧列表 if 条件]</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python">b = [<span class="hljs-string">&#x27;tom&#x27;</span>, <span class="hljs-string">&#x27;lily&#x27;</span>, <span class="hljs-string">&#x27;hom&#x27;</span>]<br>r = [name <span class="hljs-keyword">for</span> name <span class="hljs-keyword">in</span> b <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(name) &gt; <span class="hljs-number">3</span>]<br><span class="hljs-built_in">print</span>(r)<br>r = [name.capitalize() <span class="hljs-keyword">for</span> name <span class="hljs-keyword">in</span> b <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(name) &gt; <span class="hljs-number">3</span>]<br><span class="hljs-built_in">print</span>(r)<br></code></pre></td></tr></table></figure>

<p>关于列表推导式的详细介绍, 可以看博客文章: <a href="https://walker_kwu.gitee.io/my-blog/2022/07/21/python/advanced/%E6%8E%A8%E5%AF%BC%E5%BC%8F/">https://walker_kwu.gitee.io/my-blog/2022/07/21/python/advanced/%E6%8E%A8%E5%AF%BC%E5%BC%8F/</a></p>
<h1 id="字典"><a href="#字典" class="headerlink" title="字典"></a>字典</h1><p>通过{}括起来，元素以key:value(键值对)的方式来表示，元素与元素之间通过,隔开</p>
<blockquote>
<p>key必须是唯一的，value可以不唯一<br>key必须是不可变数据类型,比如key可以为number/string</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 方式一：</span><br>dic = <span class="hljs-built_in">dict</span>(((<span class="hljs-string">&#x27;one&#x27;</span>, <span class="hljs-number">1</span>), (<span class="hljs-string">&#x27;two&#x27;</span>, <span class="hljs-number">2</span>), (<span class="hljs-string">&#x27;three&#x27;</span>, <span class="hljs-number">3</span>)))<br><span class="hljs-built_in">print</span>(dic)  <span class="hljs-comment"># &#123;&#x27;one&#x27;: 1, &#x27;two&#x27;: 2, &#x27;three&#x27;: 3&#125;</span><br><span class="hljs-comment"># 方式二：</span><br>dic = <span class="hljs-built_in">dict</span>(one=<span class="hljs-number">1</span>, two=<span class="hljs-number">2</span>, three=<span class="hljs-number">3</span>)<br><span class="hljs-built_in">print</span>(dic)<br><span class="hljs-comment"># 方式三：</span><br>dic = <span class="hljs-built_in">dict</span>(&#123;<span class="hljs-string">&#x27;one&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;two&#x27;</span>: <span class="hljs-number">2</span>, <span class="hljs-string">&#x27;three&#x27;</span>: <span class="hljs-number">3</span>&#125;)<br><span class="hljs-built_in">print</span>(dic)<br><span class="hljs-comment"># 字典的嵌套</span><br>dic = &#123;<br>    <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;汪峰&#x27;</span>,<br>    <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">48</span>,<br>    <span class="hljs-string">&#x27;wife&#x27;</span>: [&#123;<span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;国际章&#x27;</span>, <span class="hljs-string">&#x27;age&#x27;</span>: <span class="hljs-number">38</span>&#125;,],<br>    <span class="hljs-string">&#x27;children&#x27;</span>: &#123;<span class="hljs-string">&#x27;girl_first&#x27;</span>: <span class="hljs-string">&#x27;小苹果&#x27;</span>,<span class="hljs-string">&#x27;girl_second&#x27;</span>: <span class="hljs-string">&#x27;小怡&#x27;</span>,<span class="hljs-string">&#x27;girl_three&#x27;</span>: <span class="hljs-string">&#x27;顶顶&#x27;</span>&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="获取字典中元素"><a href="#获取字典中元素" class="headerlink" title="获取字典中元素"></a>获取字典中元素</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-built_in">print</span>(info[<span class="hljs-string">&quot;adress&quot;</span>])   <span class="hljs-comment"># 获取没有的元素抛异常</span><br><span class="hljs-built_in">print</span>(info.get(<span class="hljs-string">&quot;adress&quot;</span>))   <span class="hljs-comment"># 获取没有的元素不会抛异常</span><br></code></pre></td></tr></table></figure>

<h2 id="修改字典某个元素"><a href="#修改字典某个元素" class="headerlink" title="修改字典某个元素"></a>修改字典某个元素</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 修改,有相同的可以修改值，没有新增</span><br>info[<span class="hljs-string">&quot;id&quot;</span>]=<span class="hljs-string">&quot;MS230&quot;</span><br><span class="hljs-built_in">print</span>(info)<br><span class="hljs-comment"># 同时有多个数据实现修改</span><br>info1=&#123;<span class="hljs-string">&#x27;name&#x27;</span>:<span class="hljs-string">&quot;郭然然&quot;</span>,<span class="hljs-string">&quot;id&quot;</span>:<span class="hljs-string">&quot;ms230&quot;</span>,<span class="hljs-string">&quot;adress&quot;</span>:<span class="hljs-string">&quot;上海&quot;</span>&#125;<br>info2=&#123;<span class="hljs-string">&#x27;name&#x27;</span>:<span class="hljs-string">&quot;郭然然2&quot;</span>,<span class="hljs-string">&quot;pre_xz&quot;</span>:<span class="hljs-string">&quot;15k&quot;</span>,<span class="hljs-string">&quot;bre_xz&quot;</span>:<span class="hljs-string">&quot;20k&quot;</span>,<span class="hljs-string">&quot;gs&quot;</span>:<span class="hljs-string">&quot;zjtd&quot;</span>&#125;<br>info1.update(info2)<br><span class="hljs-built_in">print</span>(info1)<br><span class="hljs-comment"># 删除</span><br><span class="hljs-keyword">del</span> info[<span class="hljs-string">&quot;id&quot;</span>]<br></code></pre></td></tr></table></figure>

<h2 id="常用函数-1"><a href="#常用函数-1" class="headerlink" title="常用函数"></a>常用函数</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs python">info = &#123;<span class="hljs-string">&#x27;a&#x27;</span>:<span class="hljs-number">1</span>, <span class="hljs-string">&#x27;b&#x27;</span>:<span class="hljs-number">2</span>&#125;<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">len</span>(info))    <span class="hljs-comment"># 获取字典长度</span><br><span class="hljs-comment"># info.update(dict2)  # dict2的数据更新到字典中</span><br><span class="hljs-built_in">print</span>(info.values())    <span class="hljs-comment"># 获取字典所有的value</span><br>info.popitem()  <span class="hljs-comment"># 删除字典中最后的一对键值对</span><br><span class="hljs-built_in">print</span>(info.pop(<span class="hljs-string">&#x27;hhh&#x27;</span>, <span class="hljs-string">&#x27;查不到&#x27;</span>))   <span class="hljs-comment"># 根据给定的key进行删除</span><br><span class="hljs-built_in">print</span>(info.keys())  <span class="hljs-comment"># keys列出所有键</span><br><span class="hljs-built_in">print</span>(info.items()) <span class="hljs-comment"># 同时获取key,value的值  </span><br><span class="hljs-built_in">print</span>(info.get(<span class="hljs-string">&#x27;age&#x27;</span>))  <span class="hljs-comment"># 取出指定键的值，没有的元素不会抛异常</span><br><span class="hljs-built_in">print</span>(&#123;&#125;.fromkeys(<span class="hljs-string">&#x27;name&#x27;</span>, <span class="hljs-number">100</span>)) <span class="hljs-comment"># 创建字典</span><br>a = info.setdefault(<span class="hljs-string">&#x27;sex&#x27;</span>, <span class="hljs-string">&#x27;M&#x27;</span>) <span class="hljs-comment"># 类似get方法，获取元素的值，但是当key不存在，则添加key并，默认为None，否则取默认值</span><br><span class="hljs-built_in">print</span>(a)<br>b = info.setdefault(<span class="hljs-string">&#x27;sex&#x27;</span>, <span class="hljs-string">&#x27;F&#x27;</span>)<br><span class="hljs-built_in">print</span>(b)<br><span class="hljs-built_in">print</span>(info.copy())  <span class="hljs-comment"># 复制字典，浅copy</span><br>info.clear() <span class="hljs-comment"># 清空内容</span><br><span class="hljs-built_in">print</span>(info)<br></code></pre></td></tr></table></figure>

<h1 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h1><p>容器型的数据类型，它要求它里面的元素是不可变的数据，但是它本身是可变的数据类型。集合是无序<br>的，一般用来去重</p>
<h2 id="集合的创建"><a href="#集合的创建" class="headerlink" title="集合的创建"></a>集合的创建</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs python">set1 = <span class="hljs-built_in">set</span>(&#123;<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-string">&#x27;Barry&#x27;</span>, <span class="hljs-literal">False</span>&#125;)<br>set1 = &#123;<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">51</span>,<span class="hljs-number">5</span>,(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>),<span class="hljs-string">&quot;yangyang&quot;</span>&#125;<br><span class="hljs-built_in">print</span>(set1)<br><span class="hljs-comment"># 空集合：</span><br>set1 = <span class="hljs-built_in">set</span>()<br><span class="hljs-built_in">print</span>(set1)<br><span class="hljs-comment"># 列表的去重 ***</span><br>l1 = [<span class="hljs-number">1</span>,<span class="hljs-string">&#x27;太白&#x27;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-string">&#x27;太白&#x27;</span>,<span class="hljs-number">2</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">3</span>, <span class="hljs-string">&#x27;太白&#x27;</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, ]<br>set1 = <span class="hljs-built_in">set</span>(l1)<br>l1 = <span class="hljs-built_in">list</span>(set1)<br><span class="hljs-built_in">print</span>(l1)<br></code></pre></td></tr></table></figure>

<h2 id="集合运算符"><a href="#集合运算符" class="headerlink" title="集合运算符"></a>集合运算符</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs python">set1 = &#123;<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>&#125;<br>set2 = &#123;<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>&#125;<br><span class="hljs-built_in">print</span>(set1 &amp; set2)  <span class="hljs-comment"># 交集,取出两个集合都存在的内容</span><br><span class="hljs-built_in">print</span>(set1 | set2)  <span class="hljs-comment"># 并集,将两个集合去重合并</span><br><span class="hljs-built_in">print</span>(set1 - set2)  <span class="hljs-comment"># 差集,第一个集合去掉和第二个集合相同的内容</span><br><span class="hljs-built_in">print</span>(set1 ^ set2)  <span class="hljs-comment"># 反交集,合并两个集合并且都去掉重合的内容,把脚踩2只船的人T出去</span><br>set1 = &#123;<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>&#125;<br>set2 = &#123;<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>&#125;<br><span class="hljs-built_in">print</span>(set1 &lt; set2)  <span class="hljs-comment"># 子集</span><br><span class="hljs-built_in">print</span>(set2 &gt; set1)  <span class="hljs-comment"># 超集</span><br></code></pre></td></tr></table></figure>


<h1 id="数据类型的可变-不可变"><a href="#数据类型的可变-不可变" class="headerlink" title="数据类型的可变/不可变"></a>数据类型的可变/不可变</h1><ul>
<li>可变数据类型：list/dict/set，值改变，内存地址不会改变。总结意思就是，程序允许该内存地址的值发生改变。</li>
<li>不可变数据类型，number/str/tuple/bool</li>
</ul>
<p>下面我们将通过解析内存地址来说明为什么数据类型分为可变和不可变</p>
<p>我们可以借助 <code>id()</code> 来查看变量的内存地址</p>
<p>首先，我们需要明确以下规则：</p>
<ul>
<li>变量不存储值，只存储值对应的内存地址</li>
<li>数据一旦被声明，就会确定其内存地址，每个值都会有其对应的内存地址</li>
<li>dict/list/set，这三种数据类型声明时，尽管长得一样，也会重新申请内存地址</li>
</ul>
<h2 id="不可变数据类型"><a href="#不可变数据类型" class="headerlink" title="不可变数据类型"></a>不可变数据类型</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs python">i = <span class="hljs-number">1</span><br><br>p = <span class="hljs-number">1</span><br><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(i))<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(p))<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(<span class="hljs-number">1</span>))<br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">结果：</span><br><span class="hljs-string">2104706885936</span><br><span class="hljs-string">2104706885936</span><br><span class="hljs-string">2104706885936</span><br><span class="hljs-string"></span><br><span class="hljs-string">可以看到，输出的结果，其内存地址都是一样的</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs python">i = <span class="hljs-string">&#x27;abc&#x27;</span><br><br>p = <span class="hljs-string">&#x27;abc&#x27;</span><br><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(i))<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(p))<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(<span class="hljs-string">&#x27;abc&#x27;</span>))<br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">结果：</span><br><span class="hljs-string">2759765138992</span><br><span class="hljs-string">2759765138992</span><br><span class="hljs-string">2759765138992</span><br><span class="hljs-string"></span><br><span class="hljs-string">可以看到，输出的结果，其内存地址都是一样的</span><br><span class="hljs-string">number/str/bool/tuple 只要声明了值，每个值的内存地址都不会改变</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs python">i = <span class="hljs-string">&#x27;abc&#x27;</span><br><br>p = <span class="hljs-string">&#x27;abc&#x27;</span><br><br><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(i))<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(p))<br><br>p = <span class="hljs-string">&quot;a&quot;</span><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(p))<br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">结果：</span><br><span class="hljs-string">1988308770352</span><br><span class="hljs-string">1988308770352</span><br><span class="hljs-string">1988308967792</span><br><span class="hljs-string"></span><br><span class="hljs-string">可以发现，只要对某个变量进行值的改变，对于程序而言，即声明一个从未有过的值</span><br><span class="hljs-string">程序就需要开辟一个内存地址来存放这个变量</span><br><span class="hljs-string"></span><br><span class="hljs-string">所以不可变数据类型的含义就是，程序不允许一个值的内存地址发生改变，如果你要想换个值，就必须得换个内存地址</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<h2 id="可变数据类型"><a href="#可变数据类型" class="headerlink" title="可变数据类型"></a>可变数据类型</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs python"><br>i = []<br><br>p = []<br><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(i))<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(p))<br><br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">结果：</span><br><span class="hljs-string">2138014044480</span><br><span class="hljs-string">2138014337664</span><br><span class="hljs-string"></span><br><span class="hljs-string">可以发现，list/dict/set在赋值到变量中时，尽管长得一样，但还是会重新声明一个内存地址</span><br><span class="hljs-string">这是为什么？我们后续介绍完其他现象后再做解答</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs python">i = []<br><br>p = []<br><br><br><span class="hljs-built_in">print</span>(i)<br><span class="hljs-built_in">print</span>(p)<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(i))<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(p))<br><br>i.append(<span class="hljs-number">1</span>)<br>p.append(<span class="hljs-number">2</span>)<br><br><span class="hljs-built_in">print</span>(i)<br><span class="hljs-built_in">print</span>(p)<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(i))<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(p))<br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">结果：</span><br><span class="hljs-string">[]</span><br><span class="hljs-string">[]</span><br><span class="hljs-string">2074530828480</span><br><span class="hljs-string">2074535315968</span><br><span class="hljs-string">[1]</span><br><span class="hljs-string">[2]</span><br><span class="hljs-string">2074530828480</span><br><span class="hljs-string">2074535315968</span><br><span class="hljs-string"></span><br><span class="hljs-string">可以发现，i和p的值，已经发生了改变，但是通过查看内存地址，可以看到其对应的内存地址还是不变</span><br><span class="hljs-string">所以可变数据类型的含义是，程序允许其值发生改变，值发生改变时，不会修改其对应的内存地址</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<p>回到我们上面遗留的一个问题：<strong>list/dict/set在赋值到变量中时，尽管长得一样，但还是会重新声明一个内存地址</strong></p>
<p>这个问题，就好似上面的i和p，在声明时都是一样的 <strong>空数组</strong></p>
<p>如果这个时候他们的内存地址都是一样的，那么怎么用 <strong>相同的内存地址来显示不同的值，如：[1] [2]</strong></p>
<p>所以我们可以把 <strong>可变数据类型</strong> 看作一个 <strong>容器</strong></p>
<p>每个容器为了存储不同的值，都会有各自的 <strong>内存空间</strong></p>
<h2 id="变量赋值的实质是：引用"><a href="#变量赋值的实质是：引用" class="headerlink" title="变量赋值的实质是：引用"></a>变量赋值的实质是：引用</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs python">i = []<br><br>p = i<br><br><span class="hljs-built_in">print</span>(i)<br><span class="hljs-built_in">print</span>(p)<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(i))<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(p))<br><br>i.append(<span class="hljs-number">1</span>)<br><br><span class="hljs-built_in">print</span>(i)<br><span class="hljs-built_in">print</span>(p)<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(i))<br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">id</span>(p))<br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">[]</span><br><span class="hljs-string">[]</span><br><span class="hljs-string">1499436372288</span><br><span class="hljs-string">1499436372288</span><br><span class="hljs-string">[1]</span><br><span class="hljs-string">[1]</span><br><span class="hljs-string">1499436372288</span><br><span class="hljs-string">1499436372288</span><br><span class="hljs-string"></span><br><span class="hljs-string">可以发现，当改变i时，p也会随着改变。</span><br><span class="hljs-string"></span><br><span class="hljs-string">因为i在声明列表时，开辟了内存空间</span><br><span class="hljs-string">把i赋值给p时，实际上是把i的内存空间赋值给p</span><br><span class="hljs-string"></span><br><span class="hljs-string">i的内存地址，引用了一个列表</span><br><span class="hljs-string">所以p也会引用这个列表</span><br><span class="hljs-string"></span><br><span class="hljs-string">所以当p或i修改这个列表的值时，都会影响引用到这个列表的变量</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>


<h1 id=""><a href="#" class="headerlink" title=""></a></h1>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/my-blog/categories/python/" class="category-chain-item">python</a>
  
  
    <span>></span>
    
  <a href="/my-blog/categories/python/Python%E5%9F%BA%E7%A1%80/" class="category-chain-item">Python基础</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/my-blog/tags/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/">#测试开发</a>
      
        <a href="/my-blog/tags/Python%E5%9F%BA%E7%A1%80/">#Python基础</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>python基础（下）</div>
      <div>https://walker_kwu.gitee.io/my-blog/2023/02/19/python/base/python基础（下）/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>木舟</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年2月19日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2023年5月23日</div>
        </div>
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="NC - 非商业性使用">
                <i class="iconfont icon-nc"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="SA - 相同方式共享">
                <i class="iconfont icon-sa"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/my-blog/2023/03/04/python/base/python%E5%88%A4%E6%96%AD%E4%B8%8E%E5%BE%AA%E7%8E%AF/" title="python判断与循环">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">python判断与循环</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/my-blog/2023/02/19/python/base/python%E5%9F%BA%E7%A1%80%EF%BC%88%E4%B8%8A%EF%BC%89/" title="python基础（上）">
                        <span class="hidden-mobile">python基础（上）</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <span>By 木舟</span> <i class="iconfont icon-love"></i> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/my-blog/js/events.js" ></script>
<script  src="/my-blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/my-blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/my-blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/my-blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
